package com.ato.IO;

import org.junit.Test;

import java.io.*;

/**
 * @author wangjing
 * @create 2021--12--09--20:46
 * 缓冲流：buffer  主要作用：加快处理文件 也是分为处理字节和字符的
 * BufferedInputStream      BufferedReader
 * BufferedOutputStream    BufferedWriter
 */
public class IOBufferTest {

    @Test
    public void copyByBufferTest() {
        long start = System.currentTimeMillis();
        BufferedInputStream bfin = null;
        BufferedOutputStream bfout = null;
        try {
            File file = new File("C:\\Users\\wangjing\\Desktop\\123.mp4");
            File file2 = new File("testBuffer.mp4");
//            区别：这里还是使用FileInputStream

            FileInputStream fi = new FileInputStream(file);
            FileOutputStream fo = new FileOutputStream(file2);

//            创建缓冲流
            bfin = new BufferedInputStream(fi);
            bfout = new BufferedOutputStream(fo);
//            这个可以设置为1024  因为一般复制的话可能是视频这种大的文件
            byte[] readLen = new byte[1024];
            int len;
            while ((len = bfin.read(readLen)) != -1) {
                bfout.write(readLen, 0, len);
                bfout.flush();
            }
            long end = System.currentTimeMillis();
            System.out.println("复制成功，耗时：" + (end - start));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bfin != null) bfin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bfout != null) bfout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Test
    public void BufferWriterTest() {
        long start = System.currentTimeMillis();
        BufferedReader bfin = null;
        BufferedWriter bfout = null;
        try {
            File file = new File("renameTo.txt");
            File file2 = new File("renameTo2.txt");
//            区别：这里还是使用FileInputStream

            FileReader fi = new FileReader(file);
            FileWriter fo = new FileWriter(file2);

//            创建缓冲流
            bfin = new BufferedReader(fi);
            bfout = new BufferedWriter(fo);
//            这个可以设置为1024  因为一般复制的话可能是视频这种大的文件
            /*
            char[] readLen = new char[1024];
            int len;
            while ((len = bfin.read(readLen)) != -1) {
                bfout.write(readLen, 0, len);
                bfout.flush();
            }*/
//            方法2： BufferReader独有的
            String data;
            while ((data = bfin.readLine()) != null) {
                bfout.write(data);  // data中不包含换行符 的，所有自己在要注意
                bfout.newLine( ); // 提供换号操作
            }
            long end = System.currentTimeMillis();
            System.out.println("复制成功，耗时：" + (end - start));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bfin != null) bfin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bfout != null) bfout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}